import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

export const constRoutes = [{
    path: '/login',
    name: 'login',
    component: () =>
      import ('@/views/login'),
  },
  {
    path: '/Index',
    name: 'screen',
    component: () =>
      import ('@/views/layout/layComponent/content/screen/Index'),
  },
  {
    path: '/home',
    name: 'home',
    component: () =>
      import ('@/views/homePage/index'),
  }
]

//管理组件滚动行为
const createRouter = () => new VueRouter({
  mode: 'history',
  routes: constRoutes,
  // scrollBehavior(to, from, savedPosition) {
  //   if (savedPosition) {
  //     return savedPosition
  //   } else {
  //     if (to.matched.some(m => !m.meta.scrollToTop)) {
  //       // coords will be used if no selector is provided,
  //       // or if the selector didn't match any element.
  //       return { x: 0, y: 0 }
  //     }
  //   }
  // }
})

const router = createRouter()
//替换现有router的routes
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}


//防止用户重复进入某个路由报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  next();
})

export default router